﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Net;

///Summary :
///This is the big daddy of all screens,
///it will load the actual level
///handle all the gameplay
///along with physics and network related tasks


namespace Imagine
{
    class Player
    {
        public Texture2D texture;
        public Vector2 pos;
        public Player()
        {
            texture = Engine.Instance().Content.Load<Texture2D>("rocket");
            pos = new Vector2(200,200);
        }
    }

    class GamePlayScreen : GameScreen
    {
        GamerCollection<NetworkGamer> allGamers;
        
        public GamePlayScreen()
        {}

        //For now i am creating variables here
         public override bool Init(object extraData)
         { 
            //Load the Level Data here, or whatever Init you want to do
            allGamers = NetworkManager.Instance().Session.AllGamers;
            //Engine.Instance().LoadLevel(NetworkManager.Instance().Session.SessionProperties[(int)GamePlayInitParamsIndex.eLevelName]);
            return true;
         }
         public override bool Draw(GameTime gameTime, SpriteBatch spriteBatch) 
         {
             //Engine.Instance().GraphicsDevice.Clear(Color.Tomato);
             spriteBatch.DrawString(Engine.Instance().SpriteFont, "GamePlayScreen", new Vector2(200, 0), Color.Chocolate);
             spriteBatch.DrawString(Engine.Instance().SpriteFont, "OMG, this game is so Damn GOOD :)", new Vector2(450, 0), Color.Chocolate);
            
             //Draw All Gamers
             DrawAllPlayers();
             return true;
         }

         public override bool Update(GameTime gameTime) 
         {
             NetworkSession session = NetworkManager.Instance().Session;

             if (session != null)
                 allGamers = session.AllGamers;                
             
             session.Update();
             
             //Now Update Local Gamers,
             foreach ( LocalNetworkGamer gamer in session.LocalGamers )
             {
                 UpdateLocalGamers(gamer);
             }
             
             //Read Other Gamers position and display them
             ReadIncomingData();
             
             return true;
         }

         public override bool Release() {return true;}

        //Reads Remote gamers new positions
        private void ReadIncomingData()
        {
            foreach (LocalNetworkGamer gamer in NetworkManager.Instance().Session.LocalGamers)
            {
                while (gamer.IsDataAvailable)
                {
                    NetworkGamer sender;

                    //else, read data
                    PacketReader reader = NetworkManager.Instance().PacketReader;
                    gamer.ReceiveData(NetworkManager.Instance().PacketReader, out sender);

                    if (sender.IsLocal)
                        continue;

                    //as denotes reference thats y its necessary, it modifies the tag itself !!
                    Player player = sender.Tag as Player;
                    
                    //Set players new position
                    player.pos = reader.ReadVector2();
                }
            }
        }

        private void UpdateLocalGamers(LocalNetworkGamer gamer)
        {
            Player player = gamer.Tag as Player;

            if (InputManager.Instance().IsKeyDown(Keys.Left))
                player.pos.X -= 5;
            if (InputManager.Instance().IsKeyDown(Keys.Right))
                player.pos.X += 5;
            if (InputManager.Instance().IsKeyDown(Keys.Up))
                player.pos.Y -= 5;
            if (InputManager.Instance().IsKeyDown(Keys.Down))
                player.pos.Y += 5; 


            PacketWriter writer = NetworkManager.Instance().PacketWriter;
            //write LoaclGamers positions into the Session
            writer.Write(player.pos);
            //This gamer will now broadcast this info on the session
            gamer.SendData( writer, SendDataOptions.InOrder );
            
        }


        private void DrawAllPlayers()
        {
            foreach (NetworkGamer gamer in NetworkManager.Instance().Session.AllGamers)
            {
                Player player = gamer.Tag as Player;
                Engine.Instance().SpriteBatch.Draw(player.texture, new Rectangle((int)player.pos.X, (int)player.pos.Y, player.texture.Width, player.texture.Height), Color.White);
            }
        }

 

    }
}

